trò chơi Cannon Siege miễn phí
12.315 lượt xem;
- GameController.cs
- Game Controller /
- Scripts /
- Assets /
- project /
1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4 using System;
5 using System.IO;
6 using System.Runtime.Serialization.Formatters.Binary;
7
8 public class GameController : MonoBehaviour {
9 public static GameController instance;
10
11 public bool isMusicOn;
12 public bool isGameStartedFirstTime;
13 public bool[] levels;
14 public int[] highscore;
15 public int score;
16 public int currentLevel;
17
18 private GameData data;
19
20 void Awake(){
21 CreateInstance ();
22 }
23
24 // Use this for initialization
25 void Start () {
26 InitializeGameVariables ();
27 }
28
29 void CreateInstance(){
30 if (instance != null) {
31 Destroy (gameObject);
32 } else {
33 instance = this;
34 DontDestroyOnLoad (gameObject);
35 }
36 }
37
38 void InitializeGameVariables(){
39 Load ();
40
41
42 if (data != null) {
43 isGameStartedFirstTime = data.GetIsGameStartedFirstTime ();
44 } else {
45 isGameStartedFirstTime = true;
46 }
47
48 if (isGameStartedFirstTime) {
49 isGameStartedFirstTime = false;
50 isMusicOn = true;
51 levels = new bool[15];
52 highscore = new int[levels.Length];
53
54 levels [0] = true;
55 for (int i = 1; i < levels.Length; i++) {
56 levels [i] = false;
57 }
58
59 for (int i = 0; i < highscore.Length; i++) {
60 highscore [i] = 0;
61 }
62
63 data = new GameData ();
64
65 data.SetIsMusicOn (isMusicOn);
66 data.SetIsGameStartedFirstTime (isGameStartedFirstTime);
67 data.SetHighScore (highscore);
68 data.SetLevels (levels);
69
70 Save ();
71
72 Load ();
73 } else {
74 isGameStartedFirstTime = data.GetIsGameStartedFirstTime ();
75 isMusicOn = data.GetIsMusicOn ();
76 highscore = data.GetHighScore ();
77 levels = data.GetLevels ();
78 }
79
80 }
81
82 public void Save(){
83 FileStream file = null;
84
85 try{
86 BinaryFormatter bf = new BinaryFormatter();
87 file = File.Create(Application.persistentDataPath + "/data.dat");
88
89 if(data != null){
90 data.SetIsMusicOn(isMusicOn);
91 data.SetIsGameStartedFirstTime(isGameStartedFirstTime);
92 data.SetHighScore(highscore);
93 data.SetLevels(levels);
94 bf.Serialize(file, data);
95 }
96
97 }catch(Exception e){
98 Debug.LogException (e, this);
99 }finally{
100 if(file != null){
101 file.Close ();
102 }
103 }
104 }
105
106 public void Load(){
107 FileStream file = null;
108
109 try{
110 BinaryFormatter bf = new BinaryFormatter();
111 file = File.Open(Application.persistentDataPath + "/data.dat", FileMode.Open);
112 data = bf.Deserialize(file) as GameData;
113
114 }catch(Exception e){
115 Debug.LogException (e, this);
116 }finally{
117 if(file != null){
118 file.Close ();
119 }
120 }
121 }
122 }
123
124 [Serializable]
125 class GameData{
126 private bool isGameStartedFirstTime;
127 private bool isMusicOn;
128 private bool[] levels;
129 private int[] highscore;
130
131 public void SetIsGameStartedFirstTime(bool isGameStartedFirstTime){
132 this.isGameStartedFirstTime = isGameStartedFirstTime;
133 }
134
135 public bool GetIsGameStartedFirstTime(){
136 return this.isGameStartedFirstTime;
137 }
138
139 public void SetIsMusicOn(bool isMusicOn){
140 this.isMusicOn = isMusicOn;
141 }
142
143 public bool GetIsMusicOn(){
144 return this.isMusicOn;
145 }
146
147 public void SetHighScore(int[] highscore){
148 this.highscore = highscore;
149 }
150
151 public int[] GetHighScore(){
152 return this.highscore;
153 }
154
155 public void SetLevels(bool[] levels){
156 this.levels = levels;
157 }
158
159 public bool[] GetLevels(){
160 return this.levels;
161 }
162 }
2 using System.Collections.Generic;
3 using UnityEngine;
4 using System;
5 using System.IO;
6 using System.Runtime.Serialization.Formatters.Binary;
7
8 public class GameController : MonoBehaviour {
9 public static GameController instance;
10
11 public bool isMusicOn;
12 public bool isGameStartedFirstTime;
13 public bool[] levels;
14 public int[] highscore;
15 public int score;
16 public int currentLevel;
17
18 private GameData data;
19
20 void Awake(){
21 CreateInstance ();
22 }
23
24 // Use this for initialization
25 void Start () {
26 InitializeGameVariables ();
27 }
28
29 void CreateInstance(){
30 if (instance != null) {
31 Destroy (gameObject);
32 } else {
33 instance = this;
34 DontDestroyOnLoad (gameObject);
35 }
36 }
37
38 void InitializeGameVariables(){
39 Load ();
40
41
42 if (data != null) {
43 isGameStartedFirstTime = data.GetIsGameStartedFirstTime ();
44 } else {
45 isGameStartedFirstTime = true;
46 }
47
48 if (isGameStartedFirstTime) {
49 isGameStartedFirstTime = false;
50 isMusicOn = true;
51 levels = new bool[15];
52 highscore = new int[levels.Length];
53
54 levels [0] = true;
55 for (int i = 1; i < levels.Length; i++) {
56 levels [i] = false;
57 }
58
59 for (int i = 0; i < highscore.Length; i++) {
60 highscore [i] = 0;
61 }
62
63 data = new GameData ();
64
65 data.SetIsMusicOn (isMusicOn);
66 data.SetIsGameStartedFirstTime (isGameStartedFirstTime);
67 data.SetHighScore (highscore);
68 data.SetLevels (levels);
69
70 Save ();
71
72 Load ();
73 } else {
74 isGameStartedFirstTime = data.GetIsGameStartedFirstTime ();
75 isMusicOn = data.GetIsMusicOn ();
76 highscore = data.GetHighScore ();
77 levels = data.GetLevels ();
78 }
79
80 }
81
82 public void Save(){
83 FileStream file = null;
84
85 try{
86 BinaryFormatter bf = new BinaryFormatter();
87 file = File.Create(Application.persistentDataPath + "/data.dat");
88
89 if(data != null){
90 data.SetIsMusicOn(isMusicOn);
91 data.SetIsGameStartedFirstTime(isGameStartedFirstTime);
92 data.SetHighScore(highscore);
93 data.SetLevels(levels);
94 bf.Serialize(file, data);
95 }
96
97 }catch(Exception e){
98 Debug.LogException (e, this);
99 }finally{
100 if(file != null){
101 file.Close ();
102 }
103 }
104 }
105
106 public void Load(){
107 FileStream file = null;
108
109 try{
110 BinaryFormatter bf = new BinaryFormatter();
111 file = File.Open(Application.persistentDataPath + "/data.dat", FileMode.Open);
112 data = bf.Deserialize(file) as GameData;
113
114 }catch(Exception e){
115 Debug.LogException (e, this);
116 }finally{
117 if(file != null){
118 file.Close ();
119 }
120 }
121 }
122 }
123
124 [Serializable]
125 class GameData{
126 private bool isGameStartedFirstTime;
127 private bool isMusicOn;
128 private bool[] levels;
129 private int[] highscore;
130
131 public void SetIsGameStartedFirstTime(bool isGameStartedFirstTime){
132 this.isGameStartedFirstTime = isGameStartedFirstTime;
133 }
134
135 public bool GetIsGameStartedFirstTime(){
136 return this.isGameStartedFirstTime;
137 }
138
139 public void SetIsMusicOn(bool isMusicOn){
140 this.isMusicOn = isMusicOn;
141 }
142
143 public bool GetIsMusicOn(){
144 return this.isMusicOn;
145 }
146
147 public void SetHighScore(int[] highscore){
148 this.highscore = highscore;
149 }
150
151 public int[] GetHighScore(){
152 return this.highscore;
153 }
154
155 public void SetLevels(bool[] levels){
156 this.levels = levels;
157 }
158
159 public bool[] GetLevels(){
160 return this.levels;
161 }
162 }
Use this for initialization